using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Nvigorate.Repository
{
    public class SearchCriteria<TModel>
    {
        private List<Expression<Func<TModel, bool>>> _criteria = new List<Expression<Func<TModel, bool>>>();

        public SearchCriteria<TModel> Add(Expression<Func<TModel, bool>> clause)
        {
            _criteria.Add(clause);
            return this;
        }

        public IQueryable<TModel> Filter(IQueryable<TModel> query)
        {
            return Filter(query, new Queue<Expression<Func<TModel, bool>>>(_criteria));
        }

        protected IQueryable<TModel> Filter(IQueryable<TModel> query, Queue<Expression<Func<TModel, bool>>> filters)
        {
            return filters.Count == 0 ? query : Filter(query.Where(filters.Dequeue()), filters);
        }
    }
}